<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>微博详情页面</h1>
    <h2>{{weibo.nickname}}:{{weibo.content}}</h2>
    <p>发布于:{{weibo.created}}</p>
    <img v-for="url in weibo.urls.split(',')" width="200" :src="url" alt="">
    <hr>
    <div v-for="c in arr">
        <h4>{{c.nickname}} 评论道: {{c.content}}
            <span style="float: right">发布于:{{c.created}}</span>
        </h4>
    </div>
    <input type="text" placeholder="评论内容..." v-model="comment.content">
    <input type="button" value="评论" @click="insert()">
</div>
<!--引入axios和vue框架文件-->
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            weibo: {urls: ""},
            comment: {content: ""},
            arr: []
        },
        methods: {
            insert() {
                //发布评论时需要把当前浏览的微博id添加到评论对象里面
                v.comment.weiboId = v.weibo.id;
                //发请求 发布评论
                axios.post("/comment/insert", v.comment).then(function (response) {
                    if (response.data == 1) {
                        location.reload();//刷新页面
                    } else {
                        alert("请先登录!");
                        location.href = "/login.html";
                    }
                })
            }
        },
        created: function () {
            //发请求获取微博详情
            axios.get("/weibo/selectById" + location.search).then(function (response) {
                v.weibo = response.data;
            })
            //发请求获取所有当前微博相关的评论
            axios.get("/comment/selectByWeiboId" + location.search).then(function (response) {
                v.arr = response.data;
            })
        }
    })
</script>
</body>
</html>